package com.seu.dm.controllers;

import com.seu.dm.entities.Order;
import com.seu.dm.services.OrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Greeting on 2017/2/28.
 * 商品相关的控制层逻辑，根据方法使用者的身份添加不同的权限
 * SellerPermission,BuyerPermission,游客默认无权限
 */
@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 从后台取出销量前num位的商品
     * @param num
     * @param model
     * @return
     */
    @RequestMapping(value = "/hotProduct")
    public String findHotProductsFromOrder(@RequestParam(value = "num")Integer num, Model model) {
        HashMap<String, Integer> productNameAndNum = orderService.findHotProductsFromOrder(num);
        for (String s : productNameAndNum.keySet()) {
            System.out.println(s + ": " + productNameAndNum.get(s));
        }
        model.addAttribute("map", productNameAndNum);
        return "testOrder";
    }

    /**
     * 根据id得到对应商品
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/findOrder")
    public String findOrderById(@Param(value = "id") Integer id, Model model){
        Order order = orderService.findOrder(id);
        model.addAttribute("order", order);
        return "seller/transaction_manage";
    }
}
